18. Exercise: Make an AsyncTask

Make an AsyncTask

Optional Exercise Code

Exercise: T08.02-Exercise-AddAsyncTaskToRetrieveCursor

If you’d like the skip this, you can simply check out the following branch and review the diff TODO at the bottom.

If you’d like to do it yourself, the end goal is to make an AsyncTask that loads the data on a background thread in a Cursor object. It then updates a variable called mData in the MainActivity to store the returned Cursor.

Steps to get the data from the Content Provider and save it into an instance variable

  1. Create an AsyncTask with the following generic types <Void, Void, Cursor>
  2. In the doInBackground method, write the code to access the DroidTermsExample provider and return the Cursor object.
  3. Create an instance variable Cursor mData.
  4. In the onPostExecute method, store the Cursor object in mData.
  5. Create and execute the AsyncTask in onCreate.

Solution: [T08.02-Solution-AddAsyncTaskToRetrieveCursor][Diff]